﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using qlqa.Models.LinQ;

namespace qlqa.Delegate.TableDelegate
{
    public class TableDelegate
    {
        readonly DatabaseDataContext _database = new DatabaseDataContext();
        readonly HoaDonDelegate.HoaDonDelegate _hoaDonDelegate = new HoaDonDelegate.HoaDonDelegate();
        readonly public int UserId = Convert.ToInt32(HttpContext.Current.Session["USERID"] ?? 0);

        public tb_Ban GetTable(int? id)
        {
            try
            {
                return _database.tb_Bans.FirstOrDefault(p => p.Id == id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<tb_Ban> GetTables(int? id)
        {
            try
            {
                if (id == null)
                {
                    return
                        _database.tb_Bans.Where(p => (p.IsDelete == null || p.IsDelete == false))
                            .ToList();
                }

                return _database.tb_Bans.Where(p => p.Id == id).ToList();
            }
            catch (Exception)
            {
                return new List<tb_Ban>();
            }
        }

        public List<tb_Ban> GetUsableTables()
        {
            try
            {
                return _database.tb_Bans.Where(p => (p.IsDelete == null || p.IsDelete == false) && p.IsSuDung == true && (p.TrangThaiSuDung == 1 || p.TrangThaiSuDung == null)).ToList();
            }
            catch (Exception)
            {
                return new List<tb_Ban>();
            }
        }

        public List<meta_TrangThaiSuDung_Ban> GetTrangThaiSuDungBan()
        {
            try
            {
                return _database.meta_TrangThaiSuDung_Bans.Where(p=>p.IsShow == true).ToList();
            }
            catch (Exception)
            {
                return new List<meta_TrangThaiSuDung_Ban>();
            }
        }

        public bool InsertTable(tb_Ban obj)
        {
            try
            {
                _database.tb_Bans.InsertOnSubmit(obj);
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public tb_Ban InsertTableReturnTable(tb_Ban obj)
        {
            try
            {
                _database.tb_Bans.InsertOnSubmit(obj);
                _database.SubmitChanges();
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool DeleteTable(tb_Ban obj)
        {
            try
            {
                if (obj.TrangThaiSuDung == 2)
                {
                    return false;
                }
                obj.IsDelete = true;
                obj.NguoiSua = UserId;
                obj.NgaySua = DateTime.Now;
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool SaveChange()
        {
            try
            {
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool ChuyenTrangThai(int idBan,int i)
        {
            try
            {
                var ban = _database.tb_Bans.FirstOrDefault(p => p.Id == idBan);
                if (ban != null) ban.TrangThaiSuDung = i;
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public List<tb_Ban> GetMergeTables(int idBanChinh)
        {
            try
            {
                var hoaDon = _database.tb_HoaDons.Where(
                    p =>
                        p.IdBan == idBanChinh && (p.IsThanhToan == false || p.IsThanhToan == null) && (p.IsDelete == false ||
                        p.IsDelete == null)).OrderByDescending(p=>p.Id).FirstOrDefault();

                if (hoaDon == null) return new List<tb_Ban>();

                if (string.IsNullOrEmpty(hoaDon.ArrayBan)) return new List<tb_Ban>();

                var list = new List<tb_Ban>();

                var array = hoaDon.ArrayBan.Split(',');
                if (!array.Any()) return list;
                foreach (var s in array)
                {
                    int num1;
                    var res = int.TryParse(s, out num1);
                    if (!res) continue;
                    var ban = _database.tb_Bans.FirstOrDefault(p => p.Id == num1);
                    if (ban != null)
                    {
                        list.Add(ban);
                    }
                }

                return list;
            }
            catch (Exception)
            {
                return new List<tb_Ban>();
            }
        }

        public bool ChuyenTrangThaiGhepBan(int? idBanCha, int idBan, int i)
        {
            try
            {
                if (idBanCha != null)
                {
                    var ban = _database.tb_Bans.FirstOrDefault(p => p.Id == idBan);
                    if (ban != null)
                    {
                        ban.IdBan = idBanCha;
                        ban.TrangThaiSuDung = i;
                        _database.SubmitChanges();
                        return true;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteMergedTable(long? idHoaDon, int? idBan, int? idBanRemove)
        {
            try
            {
                var hoaDon = _hoaDonDelegate.GetHoaDon(idHoaDon);
                var ban = GetTable(idBanRemove);
                var str = string.Empty;
                if (hoaDon != null)
                {
                    if (hoaDon.IdBan == idBan)
                    {
                        if (!string.IsNullOrEmpty(hoaDon.ArrayBan))
                        {
                            var array = hoaDon.ArrayBan.Split(',');
                            if (array.Any())
                            {
                                var newarray = array.Where(p => p != idBanRemove.ToString()).ToArray();
                                str = string.Join(",", newarray);

                                hoaDon.ArrayBan = str;
                                ban.IdBan = 0;
                                ban.TrangThaiSuDung = 1;
                                _database.SubmitChanges();
                            }
                        }
                    }
                    else return false;
                }
                else return false;
                
                var hoaDon2 = _hoaDonDelegate.GetHoaDon(idHoaDon);
                hoaDon2.ArrayBan = str;
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}